package com.ruoyi.mushcoin.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryTable;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.orm.core.page.PageQuery;
import com.ruoyi.common.orm.core.page.TableDataInfo;
import com.ruoyi.common.orm.core.service.impl.BaseServiceImpl;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.mushcoin.domain.TgWalletOrderTransaction;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.mushcoin.mapper.TgUserGameCurrencyMapper;
import com.ruoyi.mushcoin.domain.TgUserGameCurrency;
import com.ruoyi.mushcoin.domain.bo.TgUserGameCurrencyBo;
import com.ruoyi.mushcoin.domain.vo.TgUserGameCurrencyVo;
import com.ruoyi.mushcoin.service.ITgUserGameCurrencyService;

import static com.ruoyi.mushcoin.domain.table.TgUserGameCurrencyTableDef.TG_USER_GAME_CURRENCY;

/**
 * TG用户游戏币Service业务层处理
 *
 * @author billy
 * 2024-11-09
 */
@Service
public class TgUserGameCurrencyServiceImpl extends BaseServiceImpl<TgUserGameCurrencyMapper, TgUserGameCurrency> implements ITgUserGameCurrencyService {
    @Resource
    private TgUserGameCurrencyMapper tgUserGameCurrencyMapper;

    @Override
    public QueryWrapper query() {
        return super.query().from(TG_USER_GAME_CURRENCY);
    }

    private QueryWrapper buildQueryWrapper(TgUserGameCurrencyBo tgUserGameCurrencyBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(TG_USER_GAME_CURRENCY.USER_ID.eq(tgUserGameCurrencyBo.getUserId()));
        queryWrapper.and(TG_USER_GAME_CURRENCY.GOLD.between(tgUserGameCurrencyBo.getParams().get("beginGold"), tgUserGameCurrencyBo.getParams().get("endGold")));
        queryWrapper.and(TG_USER_GAME_CURRENCY.DIAMOND.between(tgUserGameCurrencyBo.getParams().get("beginDiamond"), tgUserGameCurrencyBo.getParams().get("endDiamond")));
        queryWrapper.and(TG_USER_GAME_CURRENCY.RANK_COIN.between(tgUserGameCurrencyBo.getParams().get("beginRankCoin"), tgUserGameCurrencyBo.getParams().get("endRankCoin")));

        return queryWrapper;
    }

    /**
     * 查询TG用户游戏币
     *
     * @param id TG用户游戏币主键
     * @return TG用户游戏币
     */
    @Override
    public TgUserGameCurrencyVo selectById(Long id) {
        return this.getOneAs(query().where(TG_USER_GAME_CURRENCY.ID.eq(id)), TgUserGameCurrencyVo.class);

    }

    /**
     * 查询TG用户游戏币列表
     *
     * @param tgUserGameCurrencyBo TG用户游戏币Bo
     * @return TG用户游戏币集合
     */
    @Override
    public List<TgUserGameCurrencyVo> selectList(TgUserGameCurrencyBo tgUserGameCurrencyBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgUserGameCurrencyBo);
        return this.listAs(queryWrapper, TgUserGameCurrencyVo.class);
    }

    /**
     * 分页查询TG用户游戏币列表
     *
     * @param tgUserGameCurrencyBo TG用户游戏币Bo
     * @return 分页TG用户游戏币集合
     */
    @Override
    public TableDataInfo<TgUserGameCurrencyVo> selectPage(TgUserGameCurrencyBo tgUserGameCurrencyBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(tgUserGameCurrencyBo);
        queryWrapper.select("t1.*"
            , "concat(user.first_name,' ',user.last_name) realName","user.mushcoin","user.invite_rewards_total");
        queryWrapper.from(TgUserGameCurrency.class).as("t1");
        queryWrapper.leftJoin(new QueryTable("tg_user_info").as("user")).on("user.user_id=t1.user_id");
        Page<TgUserGameCurrencyVo> page = this.pageAs(PageQuery.build(), queryWrapper, TgUserGameCurrencyVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增TG用户游戏币
     *
     * @param tgUserGameCurrencyBo TG用户游戏币Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(TgUserGameCurrencyBo tgUserGameCurrencyBo) {
        TgUserGameCurrency tgUserGameCurrency = MapstructUtils.convert(tgUserGameCurrencyBo, TgUserGameCurrency.class);


        return this.save(tgUserGameCurrency);//使用全局配置的雪花算法主键生成器生成ID值
    }

    /**
     * 新增TG用户游戏币，前台提供主键值，一般用于导入的场合
     *
     * @param tgUserGameCurrencyBo TG用户游戏币Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insertWithPk(TgUserGameCurrencyBo tgUserGameCurrencyBo) {
        TgUserGameCurrency tgUserGameCurrency = MapstructUtils.convert(tgUserGameCurrencyBo, TgUserGameCurrency.class);


        return tgUserGameCurrencyMapper.insertWithPk(tgUserGameCurrency) > 0;//前台传来主键值
    }

    /**
     * 修改TG用户游戏币
     *
     * @param tgUserGameCurrencyBo TG用户游戏币Bo
     * @return 结果:true 更新成功，false 更新失败
     */
    @Override
    public boolean update(TgUserGameCurrencyBo tgUserGameCurrencyBo) {
        TgUserGameCurrency tgUserGameCurrency = MapstructUtils.convert(tgUserGameCurrencyBo, TgUserGameCurrency.class);
        if (ObjectUtil.isNotNull(tgUserGameCurrency) && ObjectUtil.isNotNull(tgUserGameCurrency.getId())) {
            boolean updated = this.updateById(tgUserGameCurrency);
            return updated;
        }
        return false;
    }


    /**
     * 批量删除TG用户游戏币
     *
     * @param ids 需要删除的TG用户游戏币主键集合
     * @return 结果:true 删除成功，false 删除失败
     */
    @Transactional
    @Override
    public boolean deleteByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }

}
